package com.example.administrator.mobliesafe;

import android.app.Activity;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.mobilesafe.utils.NumberAddressQuery;

public class NumberAddressActivity extends Activity {
    private Vibrator vibrator;
    EditText et_query_address;
    TextView tv_query_address;
    Button bt_query_address;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_number_address);
        vibrator =(Vibrator) getSystemService(VIBRATOR_SERVICE);
        et_query_address = (EditText) findViewById(R.id.et_number_address);
        tv_query_address = (TextView) findViewById(R.id.tv_number_address);
        bt_query_address = (Button) findViewById(R.id.bt_number_address);
        et_query_address.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s!=null&&s.length()>=3){
                    String address = NumberAddressQuery.queryNumberAddress(s.toString());
                    tv_query_address.setText(address);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        bt_query_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String number = et_query_address.getText().toString().trim();
                if (TextUtils.isEmpty(number)){
                    long[] longs = {200,200,300,300,1000,2000};
                    vibrator.vibrate(longs,-1);
                    Toast.makeText(NumberAddressActivity.this,"号码不能为空",Toast.LENGTH_SHORT).show();
                    Animation shake = AnimationUtils.loadAnimation(NumberAddressActivity.this,R.anim.shake);
                    et_query_address.startAnimation(shake);
                }else {
                    String address = NumberAddressQuery.queryNumberAddress(number);
                    tv_query_address.setText(address);
                }
            }
        });

    }
}
